package com.lemniscate.audio_and_video_demo.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import com.lemniscate.audio_and_video_demo.R;

/**
 * Created by l on 2018/2/27.
 * https://www.cnblogs.com/android100/p/android-surfaceView.html
 */

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback, View.OnTouchListener {

    private SurfaceHolder holder;
    private MyThread myThread;
    private Bitmap icon;

    public MySurfaceView(Context context) {
        this(context, null);
    }

    public MySurfaceView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        holder = this.getHolder();
        holder.addCallback(this);
        myThread = new MyThread(holder);

        icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        rect = new Rect(0, 0, icon.getWidth(), icon.getHeight());

        this.setOnTouchListener(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        myThread.isRun = true;
        myThread.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        myThread.isRun = false;
    }

    private Point point = new Point();//click point
    private Rect rect;//pic's rect
    private boolean canDrag;
    private int offsetX = 0, offsetY = 0;

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                point.x = (int) motionEvent.getX();
                point.y = (int) motionEvent.getY();
                if (rect.contains(point.x, point.y)) {
                    canDrag = true;
                    offsetX = point.x - rect.left;
                    offsetY = point.y - rect.top;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (canDrag) {
                    rect.left = (int) (motionEvent.getX() - offsetX);
                    rect.top = (int) (motionEvent.getY() - offsetY);
                    rect.right = rect.left + icon.getWidth();
                    rect.bottom = rect.top + icon.getHeight();
                    if (rect.left < 0) {
                        rect.left = 0;
                        rect.right = rect.left + icon.getWidth();
                    }
                    if (rect.right > getMeasuredWidth()) {
                        rect.right = getMeasuredWidth();
                        rect.left = rect.right - icon.getWidth();
                    }
                    if (rect.top < 0) {
                        rect.top = 0;
                        rect.bottom = rect.top + icon.getHeight();
                    }
                    if (rect.bottom > getMeasuredHeight()) {
                        rect.bottom = getMeasuredHeight();
                        rect.top = rect.bottom - icon.getHeight();
                    }

                }
                break;
            case MotionEvent.ACTION_UP:
                canDrag = false;
                break;
        }
        return true;
    }

    class MyThread extends Thread {
        private SurfaceHolder holder;
        private boolean isRun;

        public MyThread(SurfaceHolder holder) {
            this.holder = holder;
            isRun = false;
        }

        @Override
        public void run() {
            super.run();
            int count = 0;
            while (isRun) {
                Canvas c = null;
                try {
                    synchronized (holder) {
                        c = holder.lockCanvas();
                        c.drawColor(Color.BLUE);
                        c.drawBitmap(icon, rect.left, rect.top, null);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (c != null) {
                        holder.unlockCanvasAndPost(c);
//                        try {
//                            Thread.sleep(20);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
                    }
                }
            }
        }
    }
}
